package com.micro.modual.extension;

import com.micro.common.MicroConstant;
import com.micro.common.biz.GeneratorCodeUtil;
import com.micro.common.util.SnowFlakeUtil;
import com.micro.modual.entity.LoginAuth;
import com.micro.modual.model.bo.login.LoginBO;
import com.micro.modual.model.bo.loginauth.LoginAuthBO;

import java.util.Date;

/**
 * <h1>用户登录授权表 延展类</h1>
 *
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:23:27
 */
public class LoginAuthExtension {

    /**
     * <h2>创建系统用户登录授权</h2>
     * @param userCode       用户编号
     * @param passWord       密码
     * @param authType       授权类别
     * @param authValue      授权类别值
     * @return
     */
    public static LoginAuth createSystemLoginAuth(String userCode, String passWord, String authType, String authValue) {
        LoginAuth loginAuth = new LoginAuth();
        loginAuth.setId(SnowFlakeUtil.getId());
        loginAuth.setLoginType((byte)1);
        loginAuth.setAuthMethod(authType);
        loginAuth.setCustomerType(MicroConstant.CustomerType.SYSTEM_USER_TYPE);
        loginAuth.setCreateTime(new Date());
        loginAuth.setCredential(passWord);
        loginAuth.setIdentifier(authValue);
        loginAuth.setCustomerCode(userCode);
        return loginAuth;
    }

    /**
     * <h2>转换: 用户登录授权表 新增</h2>
     *
     * @param loginAuthBO {@link LoginAuthBO} 用户登录授权表 新增/修改 请求对象
     * @return {@link LoginAuth} 用户登录授权表
     */
    public static LoginAuth converAddLoginAuth(LoginAuthBO loginAuthBO) {
        LoginAuth loginAuth = new LoginAuth();
        loginAuth.setId(SnowFlakeUtil.getId());
        loginAuth.setCustomerCode(loginAuthBO.getCustomerCode());
        loginAuth.setCustomerType(loginAuthBO.getCustomerType());
        loginAuth.setAuthMethod(loginAuthBO.getAuthMethod());
        loginAuth.setLoginType(loginAuthBO.getLoginType());
        loginAuth.setIdentifier(loginAuthBO.getIdentifier());
        loginAuth.setCredential(loginAuthBO.getCredential());
        loginAuth.setCreateTime(loginAuthBO.getCreateTime());
        loginAuth.setUpdateTime(loginAuthBO.getUpdateTime());
        return loginAuth;
    }

    /**
     * <h2>转换: 用户登录授权表 修改</h2>
     *
     * @param loginAuthBO {@link LoginAuthBO} 用户登录授权表 新增/修改 请求对象
     * @return {@link LoginAuth} 用户登录授权表
     */
    public static LoginAuth converEditLoginAuth(LoginAuthBO loginAuthBO) {
        LoginAuth loginAuth = new LoginAuth();
        loginAuth.setId(loginAuthBO.getId());
        loginAuth.setCustomerCode(loginAuthBO.getCustomerCode());
        loginAuth.setCustomerType(loginAuthBO.getCustomerType());
        loginAuth.setAuthMethod(loginAuthBO.getAuthMethod());
        loginAuth.setLoginType(loginAuthBO.getLoginType());
        loginAuth.setIdentifier(loginAuthBO.getIdentifier());
        loginAuth.setCredential(loginAuthBO.getCredential());
        loginAuth.setCreateTime(loginAuthBO.getCreateTime());
        loginAuth.setUpdateTime(loginAuthBO.getUpdateTime());
        return loginAuth;
    }

    /**
     * <h2>创建登录授权</h2>
     * @param userCode
     * @param loginBO
     * @return
     */
    public static LoginAuth createLoginAuth(String userCode, LoginBO loginBO) {
        LoginAuth loginAuth = new LoginAuth();
        loginAuth.setId(SnowFlakeUtil.getId());
        loginAuth.setCustomerCode(userCode);
        loginAuth.setCustomerType(loginBO.getUserType());
        loginAuth.setAuthMethod(loginBO.getThirdType());
        loginAuth.setCreateTime(new Date());
        loginAuth.setLoginType(MicroConstant.AuthType.thirdAuthList.contains(loginBO.getThirdType())?(byte)2:(byte)1);
        loginAuth.setIdentifier(loginBO.getLoginName());
        return loginAuth;
    }
}
